<?php

defined('BASEPATH') or die('forbidden');

class ActivityCfg {

    public $activity_type = array();
    public $activity_time_cfg = array();
    public $activity_not = array();
    public $day_time = 86400;  /// 1天多少秒;
    public $end_time = 1767110400;  /// 结束时间;

    function __construct() {
    }

    function _init() {
    }

    function activity_cfg()
    {
        ##全部手动进行处理的活动
        $this->activity_not = array(1002, 1300, 1304, 1305, 1401, 1102, 1103);  ///  体力补给 荣耀之巅 阵营战
    
        ##[标识] = {开始类型，开始时间，结束类型，结束时间}
        #开始类型，结束类型：0 不做相关操作
        #                   1 指定时间（1767110400 时间戳）
        #                   2 指定天数（1天）
        $this->activity_type = array(
            1 => array('s_type' => 0, 's_time' => 0, 'e_type' => 2, 'e_time' => 3),   /// 加3天
            2 => array('s_type' => 0, 's_time' => 0, 'e_type' => 2, 'e_time' => 7),   /// 加7天
            3 => array('s_type' => 2, 's_time' => 1, 'e_type' => 1, 'e_time' => 1767110400),   /// 第2天开始
            4 => array('s_type' => 0, 's_time' => 0, 'e_type' => 2, 'e_time' => 10),   /// 加10天
            5 => array('s_type' => 2, 's_time' => 7, 'e_type' => 1, 'e_time' => 1767110400),   /// 第8天开始 
            6 => array('s_type' => 2, 's_time' => 3, 'e_type' => 2, 'e_time' => 3),   /// 第4天开始 加3天
            7 => array('s_type' => 2, 's_time' => 3, 'e_type' => 1, 'e_time' => 1767110400),   /// 第4天开始
			8 => array('s_type' => 0, 's_time' => 0, 'e_type' => 2, 'e_time' => 40),   ///加40天
        );

        ##[标识] = {类型，活动ID}
        $this->activity_time_cfg = array(
            1 => array(1019, 1023,1006),   /// 战力排行 招财系统 命运轮盘
            2 => array(1009),   /// 副本集字
            3 => array(1014),   /// 折扣贩售
            4 => array(1020),   /// 七天狂欢
            5 => array(1017),   /// 累计登录
            6 => array(1021),   /// 限时神宠
            7 => array(1003),   /// 感恩回馈
			8 => array(1011),   /// 开服嘉年华
        );
    }

    function updateStartTime($area_id, $start_time, $ipaddress = "", $robotgrow = false) {
    
        if(true == $robotgrow and 0 < strlen($ipaddress))
        {
            /// 机器人挂启
            $this->robot_suspended($ipaddress);
        }
    	#var_dump($start_time);
        #var_dump('updateStartTime 已经执行！');
        #exit(0);
		
        $this->activity_cfg();
	
        $area_host = get_area_list_url();
        foreach ($area_host as $k => $w)
        {
            if($area_id == $k)
            {
                $area_host_url = $area_host[$k];
                break;
            }
        }
		
        $activity_time_db = getTmpDbConnect(PLATFORM_ALL, 'all', $area_id, DBTYPE_GAMESERVER);

        set_time_limit(0);
        /// 游戏礼包详情
        $sql = "SELECT * FROM dynamic_activity_time order by id asc";
        $activity_time_db->query($sql);

        $topfight_loop = 1;
        $time = time();
        $uniqueid = array();
        $topfightnamechange = array();

        while ( $var = $activity_time_db->get_array ( 'default' ) )
        {
            $value['id']        = $var['id'];
            $value['isOpen']    = $var['isopen'];
            $value['isEver']    = $var['isever'];
            $value['level']     = $var['level']; 
            $value['times']     = $var['times'];
            $value['describe']  = $var['describe']; 
            $value['name']      = $var['name'];
            $value['rules']     = $var['rules'];
            $value['string_time']     =  $var['string_time'];
            $value['defaultstate']    =  $var['defaultstate'];
            $value['subid']     = $var['subid'];
            $value['sequence']  = $var['sequence'];
            $value['dayreset']  = $var['dayreset']; 
			#$value['activityid'] = $var['activityid'];
            #$value['startTime'] = $var['starttime'];
            #$value['endTime'] = $var['endtime'];

            if( in_array($var['activityid'], $this->activity_not) )
            {
            	#var_dump($topfight_loop);
                /// 
                if(1401 == $var['activityid'])
                {
                	#var_dump($topfight_loop);
                	
                    $topfight_name = $this->topfight_name_modfiy($topfight_loop, strtotime($var['starttime']), $start_time, $value, $var);
                    if(strlen($topfight_name) > 0)
                    {
                    	$value['startTime'] = strtotime($var['starttime']);
                    	$value['endTime'] = strtotime($var['endtime']);
                    	#var_dump($value['startTime']);
                    	#var_dump($value['endTime']);
                    	$value['name'] = $topfight_name;
                    	$topfightnamechange[] = $value;
                    	#var_dump($topfight_loop);
                    	#$activity_time_db->query("UPDATE dynamic_activity_time SET name='$topfight_name' WHERE id IN ({$value['id']})");
                    	$uniqueid[$value['id']] = $topfight_name;
                    	#$activity_time_db->query("UPDATE dynamic_activity_time SET name='$topfight_name' WHERE id IN ({$uniqueid})");
                    	#var_dump($value['name']);
                		#var_dump("$topfight_loop".$var['starttime'].$time);
                    	#var_dump(strtotime($var['starttime']));
                    	#var_dump($start_time);
	                    #var_dump($topfight_name);                    	
                    }
                }
                continue;
            }
			
            $value['startTime'] = $start_time;
            /// 
            $tmp_date = date("Y-m-d 0:0:0", $start_time);
            $value['startTime'] = strtotime($tmp_date);

            $tmp_date   = $this->end_time;           /// 2025/12/31 0:0:0
            $value['endTime']   = $tmp_date;
            
            $tmpCfgKey = 0;
            foreach ($this->activity_time_cfg as $k => $w)
            {
                #var_dump("++++++++++++++++++++++++++++++++++++++++++");
                #var_dump($var['activityid'] . " " . $k . " " . $w);
                /// 不能全部是开服时间
                if( in_array($var['activityid'], $w) )
                {
                    #var_dump($k);
                    $tmpCfgKey = $k;
                    break;
                }
                #var_dump("++++++++++++++++++++++++++++++++++++++++++");
            }
            if(0 != $tmpCfgKey && true == isset($this->activity_type[$tmpCfgKey]))
            {
                if(1 == $this->activity_type[$tmpCfgKey]['s_type'])
                {
                }
                else if(2 == $this->activity_type[$tmpCfgKey]['s_type'])
                {
                    /// 开始时间
                    $tmp_date = date("Y-m-d 0:0:0", $start_time);
                    $tmp_date = strtotime($tmp_date) + $this->activity_type[$tmpCfgKey]['s_time'] * $this->day_time;
                    $value['startTime']   = $tmp_date;
                }
                
                if(1 == $this->activity_type[$tmpCfgKey]['e_type'])
                {
                }
                else if(2 == $this->activity_type[$tmpCfgKey]['e_type'])
                {
                    $tmp_date = $value['startTime'] + $this->activity_type[$tmpCfgKey]['e_time'] * $this->day_time;
                    $value['endTime']   = $tmp_date;
                }
            }

            #var_dump($var['activityid']);
            #var_dump($value);

            #$activity_time_db->update("dynamic_activity_time", $var, array('id' => $var['id']));
            
            $result = vpost($area_host_url . array_to_stringparam($value) . "action=11&id={$value['id']}");
            #var_dump($value['id'] . "   " . array_to_stringparam($value));
            #exit(0);
        }

        #var_dump($uniqueid);
        foreach ($uniqueid as $key=>$values)
        {
        	$ret = $activity_time_db->query("UPDATE dynamic_activity_time SET name='$values' WHERE id IN ({$key})");	
        }

        #var_dump($topfightnamechange);
        #exit(0);
	    foreach ($topfightnamechange as $key=>$values)
		{
        	#var_dump($area_host_url);
        	#var_dump( "action=11&id={$value['id']}");
        	#var_dump(array_to_stringparam($values));
       		$result = vpost($area_host_url . array_to_stringparam($values) . "action=11&id={$value['id']}");
       		#exit(0);
        }

        #exit(0);
        set_time_limit(30);
    }

    /// 鑽ｈ��涔嬪穮
    function topfight_name_modfiy(&$topfight_loop, $startTime, $cur_time, $value, $var)
    {  
        $name = "";
        #var_dump($startTime . "   " . $cur_time);
        if($cur_time < $startTime)
        {
            $name = translate('ACTIVITY_TOPFIGHT_MATCH1') . $topfight_loop . translate('ACTIVITY_TOPFIGHT_MATCH2');
            #$name = $topfight_loop;
            #var_dump($name);
        }

        if(0 < strlen($name))
        {
            #var_dump($topfight_name);
            #var_dump($value['name'] . "   " . $value['startTime'] . "   " . $value['endTime']);
            #var_dump($var['starttime']);
            #var_dump($name);
            #$value['name']  = $name;
            #$value['startTime'] = strtotime($var['starttime']);
            #$value['endTime'] = strtotime($var['endtime']);
            #$result = vpost($area_host_url . array_to_stringparam($value) . "action=11&id={$value['id']}");
            
            $topfight_loop += 1;
        }
        
        return $name;
    }

    /// 机器人挂启
    function robot_suspended($ipaddress)
    {
        $endpos = strrpos($ipaddress,":");
        if($endpos==false)
        {
            return false;
        }
        $beginpos = 7;
        $tmp_data = substr($ipaddress, $beginpos, $endpos - $beginpos);        
        
        $address = $tmp_data;
        $service_port = 8000;
		
        $fp = fsockopen($address, $service_port, $errno, $errstr, 30);
        if (!$fp)
        {
            return false;
        }

        $send_data = "robot,270caf2e2d1fdc6b83ee8b08e78d935c,0,1,1";
        $res = fwrite($fp, $send_data);
        fclose($fp);
        #echo(time());
        return true;
    }
}
